package com.sinux.sshUser.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


import com.sinux.sshUser.entity.Department;
import com.sinux.sshUser.entity.Treedata;
import com.sinux.sshUser.entity.User;
import com.sinux.sshUser.service.DepartmentServiceI;
import com.sinux.sshUser.util.MUtil;


/**
 * @author WangGan
 * @version 2017年5月26日 上午9:16:11 
 * 
 */
@Controller
public class DepartController {
	
	@Autowired
	private DepartmentServiceI departService;
	
	
	/**
	 * 分页部门列表
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/deptab")
	public Map<String ,Object> depindex(HttpServletRequest request, HttpServletResponse response, Model model) {
		Map<String ,Object> map=new HashMap<String ,Object>();
		int pageIndex =Integer.valueOf(request.getParameter("page")); //第几页的数据  
	    int pageSize = Integer.valueOf(request.getParameter("rows")); //每页多少条数据  
	    List<Department> departs=departService.findDepartPageing(pageIndex,pageSize);
		//model.addAttribute("userList", userService.findAllUserList()); 
	    int total=departService.findAllDepartList().size();
	    map.put("total", total);
	    map.put("rows", departs);
		return map;
	}
	
	/**
	 * 模糊搜索 分页
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/seachdeptab")
	public Map<String ,Object> seachdepindex(HttpServletRequest request, HttpServletResponse response, Model model) {
		Map<String ,Object> map=new HashMap<String ,Object>();
		int pageIndex =Integer.valueOf(request.getParameter("page")); //第几页的数据  
	    int pageSize = Integer.valueOf(request.getParameter("rows")); //每页多少条数据
		String value=request.getParameter("txt");
		List<Department> departs=departService.findDepartList(value,pageIndex,pageSize);
		//model.addAttribute("departList", departService.findDepartList(value));
		int total=departService.findDepartList(value).size();
		map.put("total", total);
	    map.put("rows", departs);
		return map;
	}
	/**添加部门*/
	@RequestMapping("/add_depart")
	public Map<String ,Object> addDepart(HttpServletRequest request, HttpServletResponse response, Department depart){
		Map<String ,Object> map=new HashMap<String ,Object>();
		Department detst=depart;
		
		departService.addDepartment(depart);
		String s1=request.getParameter("depid");
		System.out.println("depid:"+s1);
		map.put("msg", "成功");
		return map;
	}
	
	
	/**
	 * 树形列表
	 * @return
	 */
	@RequestMapping("/depart_treegrid")
	public String dpttreegrid(){
		return "include/treegridlog";
	}
	
	/**
	 * 树形json
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/treejson")
	public List<Department> dftree(){
		//List<Department> delist=departService.findAllDepartList();//数据源
		List<Department> tlist=new ArrayList<Department>();//创建父节点集合
		Department tree=dgTree(MUtil.rootNumber); //递归转换
		tlist.add(tree);
		return tlist;
	}
	
	/**
	 * 递归转换
	 * @param deid
	 * @return
	 */
	public Department dgTree(String deid){
		Department depart=departService.findDepart(deid);//根据id查找节点获取单个对象
		List<Department> departs=departService.findDeparts(deid);//查找以这个节点为父节点的所有对象
		for(Department child:departs){ //遍历集合
			Department da=dgTree(child.getId());//递归查找
			depart.getChildren().add(da);	
		}
		return depart;
			
	}
	
	/**
	 * 查询部门内的用户 分页
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/sel_user_depart")
	public Map<String ,Object> selUserinDepart(HttpServletRequest request,HttpServletResponse response,Model model){
		Map<String, Object> map=new HashMap<String, Object>();
		int page =Integer.valueOf(request.getParameter("page")); //第几页的数据  
	    int rows = Integer.valueOf(request.getParameter("rows")); //每页多少条数据
	    String deid=request.getParameter("deid");//获取部门ID
	    System.out.println("page:"+page+" rows:"+rows+" deid:"+deid);
	    int total=departService.findDepart(deid).getUsers().size();
	    List<User> list=departService.findDepartUsers(deid, page, rows);
	    if(list==null){
	    	map.put("total", 0);
		    map.put("rows", null);
	    }else{
	    	map.put("total", total);
		    map.put("rows", list);
	    }
		return map;
	}
	
	
	/**
	 * 从某个部门总移除用户
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/remove_user_depart")
	public Map<String, Object> removeUser(HttpServletRequest request,HttpServletResponse response,Model model){
		Map<String, Object> map=new HashMap<String,Object>();
		Map<String, String[]> params=request.getParameterMap();
		String deid=params.get("deid")[0];
		String ss=params.get("uid")[0];
		String[] uids=ss.split(",");
		boolean isTAG=departService.removeDepartUsers(uids, deid);
		if(isTAG){
			map.put("success", true);
			map.put("msg", "成功");
		}else{
			map.put("success", false);
			map.put("msg", "失败");
		}
		return map;
	}
	
}
